package com.huike.web.controller.review;


import com.huike.clues.domain.TbClue;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.review.pojo.Review;
import com.huike.review.service.ReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 该Controller主要是为了复习三层架构以及Mybatis使用的，该部分接口已经放开权限，可以直接访问
 * 同学们在此处编写接口通过浏览器访问看是否能完成最简单的增删改查
 */
@RestController
@RequestMapping("/review")
public class MybatisReviewController extends BaseController {

    @Autowired
    private ReviewService reviewService;

    /**=========================================================新增数据============================================*/
    @PostMapping("/saveData")
    public AjaxResult save(@RequestBody Review review){
        reviewService.savaData(review);
        AjaxResult r = new AjaxResult(200, "成功插入:1条数据");
        return r;
    }
    //测试路径写的不对 前端传过来的数据后端接受不到
    @GetMapping("/saveData/{name}/{age}/{sex}")
    public AjaxResult save(@PathVariable("name") String name,@PathVariable("age") Integer age,@PathVariable("sex") String sex ){
        Review review = new Review();
        review.setName(name);
        review.setAge(age);
        review.setSex(sex);
        reviewService.savaData(review);
        AjaxResult r = new AjaxResult(200, "成功插入:1条数据");
        return r;
    }
    /**=========================================================删除数据=============================================*/
    @DeleteMapping("/remove/{id}")
    public AjaxResult deleteById(@PathVariable("id") Long id){
        reviewService.deleteById(id);
        AjaxResult r = new AjaxResult(200, "成功删除:1条数据");
        return r;
    }
    /**=========================================================修改数据=============================================*/
    @PostMapping("/update")
    public AjaxResult update (@RequestBody Review review){
        reviewService.updateById(review);
        AjaxResult r = new AjaxResult(200, "修改成功");
        return r;
    }

    /**=========================================================查询数据=============================================*/
    //根据Id查询
    @GetMapping("/getById")
    public AjaxResult selectById(@RequestParam(value = "id") Long id){
        Review review = reviewService.getById(id);
        return new  AjaxResult(200, "操作成功",review);
    }
    //分页查询
    @GetMapping("/getDataByPage")
    public TableDataInfo page(){
        startPage();
        List<Review> list =reviewService.selectPage();
        return getDataTable(list);
    }

}